package com.xxx.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.xxx.config.Request;
import com.xxx.pojo.*;
import com.xxx.pojo.vo.ClientVo;
import com.xxx.service.ClientDetailService;
import com.xxx.service.ClientService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author George
 * @since 2023-07-06
 */
@Api(tags = "客户控制")
@CrossOrigin
@RestController
@RequestMapping("/client")
public class ClientController {

    @Resource
    private ClientService clientService;

    @Resource
    private ClientDetailService clientDetailService;

    /**
     * 连表查询客户信息
     *
     * @return 返回用户列表和总数
     */
    @ApiOperation(value = "根据条件查询客户列表", notes = "根据分页查询参数查询客户列表")
    @PostMapping("/clientListByCondition")
    public Request getClientListByCondition(
            @RequestParam("searchValue") String searchValue,
            @RequestParam("pageNum") int pageNum,
            @RequestParam("pageSize") int pageSize) {

        Page<ClientVo> mapPage = new Page<>(pageNum, pageSize, true);

        // 去除查询条件中的空格
        String finalSearchValue = StringUtils.isNotBlank(searchValue) ? searchValue.trim() : null;

//        左连表
        MPJLambdaWrapper<Client> lambdaWrapper = new MPJLambdaWrapper<Client>()
                .select(Client::getClientWechat,Client::getClientName,
                        Client::getClientId,Client::getClientVip,
                        Client::getDeleted,Client::getClientSuperior,
                        Client::getGmtCreation,Client::getClientBalance)
                .select(ClientDetail::getClientDetailAddress)
                .select(Label::getLabelCategory)
                .leftJoin(ClientDetail.class, ClientDetail::getClientId, Client::getClientId)
                .leftJoin(Label.class, Label::getLabelId, ClientDetail::getLabelId);

//        条件查询
        if (StringUtils.isNotBlank(finalSearchValue)) {
            lambdaWrapper.and(wrapper -> wrapper
                    .like(Client::getClientName, finalSearchValue)
                    .or()
                    .like(Client::getClientEmail, finalSearchValue)
                    .or()
                    .like(Client::getClientWechat, finalSearchValue));
        }

        clientService.selectJoinListPage(mapPage, ClientVo.class, lambdaWrapper);

        List<ClientVo> records = mapPage.getRecords();
        long total = mapPage.getTotal();

        return records.size() > 0 ? Request.suc(total,records) : Request.error("查询失败");
    }


    /**
     * 根据标签查询
     *
     * @return 返回用户列表和总数
     */
    @ApiOperation(value = "根据标签查询", notes = "根据标签查询客户列表")
    @PostMapping("/labelId")
    public Request getLabelId(
            @RequestParam("neLabelId") String neLabelId,
            @RequestParam("pageSize") int pageSize,
            @RequestParam("pageSize") int pageNum) {

        Page<ClientVo> mapPage = new Page<>(pageNum,pageSize,true);
//        左连表
        MPJLambdaWrapper<Client> lambdaWrapper = new MPJLambdaWrapper<Client>()
                .select(Client::getClientWechat)
                .select(ClientDetail::getClientDetailAddress)
                .selectAll(Label.class)
                .leftJoin(ClientDetail.class, ClientDetail::getClientId, Client::getClientId)
                .leftJoin(Label.class, Label::getLabelId, ClientDetail::getLabelId);

//        条件查询
        if (StringUtils.isNotBlank(neLabelId)) {
            lambdaWrapper.and(wrapper -> wrapper
                    .eq(ClientDetail::getLabelId,neLabelId));
        }


        clientService.selectJoinListPage(mapPage, ClientVo.class, lambdaWrapper);

        List<ClientVo> records = mapPage.getRecords();
        long total = mapPage.getTotal();
        return records.size() > 0 ? Request.suc(total,records) : Request.error("根据标签分类失败");
    }


    /**
     * 根据id查询客户详情
     *
     * @return 返回用户列表和总数
     */
    @ApiOperation(value = "根据工号查询用户详情", notes = "根据用户工号查询用户全部信息")
    @PostMapping("/clientDetailsByWorkNumber")
    public Request getClientDetailsByWorkNumber(
            @RequestParam("clientId") String clientId) {
        List<ClientVo> clientVos = clientService.selectJoinList(ClientVo.class,
                new MPJLambdaWrapper<Client>()
                        .selectAll(Client.class)
                        .selectAll(ClientDetail.class)
                        .select(Label::getLabelCategory)
                        .leftJoin(ClientDetail.class, ClientDetail::getClientId, Client::getClientId)
                        .leftJoin(Label.class,Label::getLabelId,ClientDetail::getLabelId)
                        .eq(Client::getClientId, clientId)
        );
        return clientVos.size() > 0 ? Request.suc(clientVos) : Request.error("根据客户id查询客户详情失败");
    }


    /**
     * 添加客户
     *
     * @return 返回用户列表和总数
     */
    @ApiOperation(value = "添加客户", notes = "注册添加新客户")
    @PostMapping("/addClient")
    public Request addClient(
            @RequestParam("weChatId") String weChatId,
            @RequestParam("clientEmail") String clientEmail,
            @RequestParam("pwd") String pwd) {
        // 创建Client对象并设置属性

        Client client = new Client();

        client.setClientWechat(weChatId);
        client.setClientEmail(clientEmail);
        client.setClientPwd(pwd);

        ClientDetail clientDetail = new ClientDetail();
//        执行添加
        boolean savedClient = clientService.save(client);

        clientDetail.setClientId(client.getClientId());
        clientDetail.setLabelId(101);
        boolean savedClientDetail = clientDetailService.save(clientDetail);

        if (savedClient && savedClientDetail) {
            return Request.suc();
        } else {
            return Request.error("添加用户失败");
        }
    }


    /**
     * 根据di批量拉黑用户
     *
     * @return 返回用户列表和总数
     */
    @ApiOperation(value = "批量禁用/拉黑客户", notes = "根据客户编号批量禁用/拉黑客户")
    @DeleteMapping("/disableClientsByIds")
    public Request disableClientsByIds(
            @RequestParam("clientId") List<Long> clientId) {
        System.out.println(clientId);
        boolean disabled = clientService.removeByIds(clientId);
        return disabled ? Request.suc() : Request.error("批量禁用/拉黑客户失败");
    }


    /**
     * 重置客户密码
     *
     * @param clientId   客户id
     * @return 返回操作结果
     */
    @ApiOperation(value = "重置密码", notes = "重置用户密码")
    @PutMapping("/resetClientPassword")
    public Request resetUserPassword
    (@RequestParam("clientId") Long clientId) {

        Client client = clientService.getById(clientId);

        if (client == null) {
            return Request.error("未找到该员工");
        }

        client.setClientPwd(client.getClientWechat());
        boolean updated = clientService.updateById(client);
        return updated ? Request.suc() : Request.error("重置失败");
    }


}

